package com.deliinsns.sys.util;
import org.apache.commons.lang.StringUtils;
import java.util.HashMap;
import java.util.Map;

public class SystemContext {
	/**
	 * 用于保存线程相关信息
	 */
	transient static ThreadLocal<Map<String, String>> contextMap = new ThreadLocal<Map<String, String>>();


	/**
	 * 构造函数
	 */
	public SystemContext() {
		// For Spring initialization.
	}

	/**
	 * 从 ThreadLocal中获取名值Map(不包含appCode)
	 *
	 * @return 名值Map
	 */
	public static Map<String, String> getContextMap() {
		return contextMap.get();
	}

	/**
	 * 从 ThreadLocal 获取名值Map
	 *
	 * @param contextMap 名值Map
	 */
	public static void setContextMap(Map<String, String> contextMap) {
		SystemContext.contextMap.set(contextMap);
	}

	/**
	 * （获取键下的值.如果不存在，返回null；如果名值Map未初始化，也返回null） Get the value of key. Would
	 * return null if context map hasn't been initialized.
	 *
	 * @param key 键
	 * @return 键下的值
	 */
	public static String get(String key) {
		Map<String, String> contextMap = getContextMap();
		if (contextMap == null) {
			return null;
		}

		return contextMap.get(key);
	}

	/**
	 * （设置名值对。如果Map之前为null，则会被初始化） Put the key-value into the context map;
	 * <p/>
	 * Initialize the map if the it doesn't exist.
	 *
	 * @param key   键
	 * @param value 值
	 * @return 之前的值
	 */
	public static String put(String key, String value) {
		Map<String, String> contextMap = getContextMap();
		if (contextMap == null) {
			contextMap = new HashMap<String, String>();
			setContextMap(contextMap);
		}
		return contextMap.put(key, value);
	}

	public static String[] getRoleIds() {
		String roleIds = get(SystemConstant.ROLE_ID);
		if (StringUtils.isEmpty(roleIds)) {
			return null;
		}
		return roleIds.split(",");
	}

	public static String getUserId() {
		return get(SystemConstant.USER_ID);
	}

	public static String getUserName() {
		return get(SystemConstant.USER_NAME);
	}

	public static String getUnitId() {
		return get(SystemConstant.UNIT_ID);
	}

	public static String getOrgCode() {
		String orgCode = get(SystemConstant.ORG_CODE);
		if (orgCode == null || orgCode.isEmpty()) {
			return SystemConstant.DEFAULT_ORG_CODE;
		}
		return orgCode;
	}

	public static String getLocale() {
		String locale = get(SystemConstant.LOCALE);
		if (locale == null || locale.isEmpty()) {
			return SystemConstant.DEFAULT_LOCALE;
		}
		return locale;
	}

	public static String getVersion() {
		String version = get(SystemConstant.VERSION);
		if (version == null || version.isEmpty()) {
			return SystemConstant.DEFAULT_VERSION;
		}
		return version;
	}

	public static String getClientIp() {
		return get(SystemConstant.CLIENT_IP);
	}

	public static String getThemeCode() {
		return get(SystemConstant.THEME_CODE);
	}

	public static void setClientIp(String clientIp) {
		put(SystemConstant.CLIENT_IP, clientIp);
	}

	public static void setUserId(String userId) {
		put(SystemConstant.USER_ID, userId);
	}

	public static void setUserName(String userName) {
		put(SystemConstant.USER_NAME, userName);
	}

	public static void setOrgCode(String orgCode) {
		put(SystemConstant.ORG_CODE, orgCode);
	}

	public static void setLocale(String locale) {
		put(SystemConstant.LOCALE, locale);
	}

	public static void setThemeCode(String themeCode) {
		put(SystemConstant.THEME_CODE, themeCode);
	}
}

